/*
 * 8.5
 * Implement an algorithm to print all valid (e.g., properly opened and closed) combi-
 * nations of n-pairs of parentheses.
 * EXAMPLE:
 * input: 3 (e.g., 3 pairs of parentheses)
 * output: ()()(), ()(()), (())(), ((()))
 */
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

string print(int n)
{
    string s;
    for (int i = 0; i < n; i++) {
        s += '(';
    }
    for (int i = 0; i < n; i++) {
        s += ')';
    }
    return s;
}

void combination(int n, vector<int> &v)
{
    if (n == 0) {
        bool f = false;
        for (int i = 0; i < v.size(); i++) {
            printf("%s", print(v[i]).c_str());
        }
        printf(" ");
        return;
    }
    for (int i = 1; i <= n; i++) {
        v.push_back(i);
        combination(n-i, v);
        v.erase(v.end()-1);
    }
}

int main()
{
    vector<int> v;
    combination(3, v);
    printf("\n");
    return 0;
}
